package setting.itheima.com.a01_guanli;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import setting.itheima.com.a01_guanli.db.dao.StudentDao;
import setting.itheima.com.a01_guanli.domain.Student;

public class MainActivity extends AppCompatActivity {

    EditText ed_name;
    RadioGroup rgb;
    LinearLayout ll_layout;
    StudentDao sdao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ed_name = (EditText) findViewById(R.id.ed_name);
        rgb = (RadioGroup) findViewById(R.id.rgb);
        ll_layout = (LinearLayout) findViewById(R.id.ll_layout);

        sdao = new StudentDao(this);
    }

    public void save(View view){
        String name = ed_name.getText().toString().trim();
        if (TextUtils.isEmpty(name)){
            Toast.makeText(this,"姓名不能为空",Toast.LENGTH_SHORT).show();
            return;
        }
        String sex = "male";

        int id = rgb.getCheckedRadioButtonId();
        if (id==R.id.male){
            sex="male";
        }else{
            sex="female";
        }

        StudentDao sdao = new StudentDao(this);
        sdao.insert(name,sex);

        Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();
    }

    public void delete(View view){
        String name = ed_name.getText().toString().trim();
        if (TextUtils.isEmpty(name)){
            Toast.makeText(this,"姓名不能为空",Toast.LENGTH_SHORT).show();
            return;
        }

        StudentDao sdao = new StudentDao(this);
        sdao.delete(name);

        Toast.makeText(this,"删除成功",Toast.LENGTH_SHORT).show();
    }


    public void display(View view){
        List<Student> list = sdao.getAll();

        ll_layout.removeAllViews();
        for (Student student : list){
            TextView tv = new TextView(this);
            tv.setText(student.toString());
            ll_layout.addView(tv);
        }
    }
}
